package com.prdiot.poetry.service;

import com.prdiot.poetry.dao.*;
import com.prdiot.poetry.domains.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class SearchService {

    @Autowired
    TagsService tagsService;

    @Autowired
    PoemsRepository poemsRepository;

    @Autowired
    AuthorRepository authorRepository;

    @Autowired
    MingjuRepository mingjuRepository;

    @Autowired
    ChengyuRepository chengyuRepository;

    public Map<String, Object> search(int page,String searchVal,String searchType){
        Map<String, Object> mapResult = new HashMap<>();
        List<Object> list =  new ArrayList<>();
        if (searchType.equals("poem")){
            //按照 标题 内容搜索诗词
            Sort sort = new Sort(Sort.Direction.DESC, "translation");
            Pageable pageable = new PageRequest(page - 1, 10, sort); // （当前页， 每页记录数， 排序方式）
            Page<Poems> poems = poemsRepository.searchByKey(searchVal, pageable);
            List<Poems> poemsList =  poems.getContent();

            Map<String, String> tagsMap = tagsService.getTagsMap();
            for (Poems poem: poemsList) {
                poem.setTitle(poem.getTitle().replaceAll(searchVal,"<span style='color:#f00;'>"+searchVal+"</span>"));
                poem.setBody(poem.getBody().replaceAll(searchVal,"<font color='#f00'>"+searchVal+"</font>"));
                poem.setAuthor(poem.getAuthor().replaceAll(searchVal,"<span style='color:#f00;'>"+searchVal+"</span>"));
                String[] tagNames = poem.getTag().split(",");
                Map<String, String> poemTags = new HashMap<>();
                for (String tagName: tagNames) {
                    if (tagsMap.containsKey(tagName.trim())){
                        poemTags.put(tagsMap.get(tagName.trim()),tagName.trim());
                    }
                }
                poem.setTags(poemTags);
            }

            list.clear();
            list.addAll(poemsList);
            mapResult.put("list",list);
            mapResult.put("pageCount",poems.getTotalPages());
        }else if (searchType.equals("author")){
            //搜索诗人
            Pageable pageable = new PageRequest(page - 1, 10); // （当前页， 每页记录数， 排序方式）
            Page<Author> authors = authorRepository.searchByKey(searchVal, pageable);

            List<Author> authorList =  authors.getContent();

            for (Author author:authorList) {
                author.setAuthorName(author.getAuthorName().replaceAll(searchVal,"<font color='#f00'>"+searchVal+"</font>"));
            }

            list.clear();
            list.addAll(authorList);
            mapResult.put("list",list);
            mapResult.put("pageCount",authors.getTotalPages());

            //根据诗人 推荐 诗词
            //如果诗人结果多 则不推荐诗词
            if (authorList.size() == 1){
                List<Poems> poemsList = new ArrayList<>();
                for (Author author:authorList) {
                    poemsList.addAll(poemsRepository.getRandPoetryByAuthorId(author.getAuthorId()));
                }
                if (poemsList!=null && poemsList.size()>0){
                    Map<String, String> tagsMap = tagsService.getTagsMap();
                    for (Poems poem: poemsList) {
                        String[] tagNames = poem.getTag().split(",");
                        Map<String, String> poemTags = new HashMap<>();
                        for (String tagName: tagNames) {
                            if (tagsMap.containsKey(tagName.trim())){
                                poemTags.put(tagsMap.get(tagName.trim()),tagName.trim());
                            }
                        }
                        poem.setTags(poemTags);
                    }
                }
                mapResult.put("extPomesData",poemsList);
            }

        }else if (searchType.equals("mj")){
            //搜索名句
            Pageable pageable = new PageRequest(page - 1, 10); // （当前页， 每页记录数， 排序方式）
            Page<Mingju> mingjus = mingjuRepository.searchByKey(searchVal, pageable);

            List<Mingju> mingjuList =  mingjus.getContent();
            Map<String, String> tagsMap = tagsService.getTagsMap();
            for (Mingju mingju:mingjuList) {
                mingju.setBody(mingju.getBody().replaceAll(searchVal, "<font color='#f00'>" + searchVal + "</font>"));
                String[] tagNames = mingju.getTag().split(",");
                Map<String, String> poemTags = new HashMap<>();
                for (String tagName: tagNames) {
                    if (tagsMap.containsKey(tagName.trim())){
                        poemTags.put(tagsMap.get(tagName.trim()),tagName.trim());
                    }
                }
                mingju.setTags(poemTags);
            }

            list.clear();
            list.addAll(mingjuList);
            mapResult.put("list",list);
            mapResult.put("pageCount",mingjus.getTotalPages());
        }else if (searchType.equals("cy")){
            //搜索成语
            Pageable pageable = new PageRequest(page - 1, 10); // （当前页， 每页记录数， 排序方式）
            Page<Chengyu> chengyus = chengyuRepository.searchByKey(searchVal, pageable);

            List<Chengyu> chengyuList =  chengyus.getContent();
            for (Chengyu chengyu:chengyuList) {
                if(chengyu.getChengyu() != null) {
                    chengyu.setChengyu(chengyu.getChengyu().replaceAll(searchVal, "<font color='#f00'>" + searchVal + "</font>"));
                }
                if(chengyu.getChuchu() != null){
                    chengyu.setChuchu(chengyu.getChuchu().replaceAll(searchVal,"<font color='#f00'>"+searchVal+"</font>"));
                }
            }
            list.clear();
            list.addAll(chengyuList);
            mapResult.put("list",list);
            mapResult.put("pageCount",chengyus.getTotalPages());
        }
        return mapResult;
    }

}
